package com.controller;


import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class springController {

    //页面跳转
    //直接返回字符串
    @RequestMapping("/data1.do")
    public String data1(HttpServletRequest request){
        request.setAttribute("msg","直接返回字符串");
        return "index";
    }
    @RequestMapping("/data2.do")
    public String data2(Model model){
        model.addAttribute("msg","直接返回字符串");
        return "index";
    }

    @RequestMapping("/data3.do")
    public ModelAndView data3(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("username","1234");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/data4")
    @ResponseBody
    public String data4(){
        return "{\"username\":\"zhangsan\",\"age\":18}";
    }

    @RequestMapping("/data5.do")
    @ResponseBody
    public User data5() throws IOException {
        User user=new User("张三",18);
        return user;
    }

    @RequestMapping("/data6.do")
    public void data6(HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        writer.println("通过response获取输出流在页面输出数据");
    }
}
